//有空改成無延遲式

function JumpRoutine takes nothing returns nothing
	local timer jumpTimer = GetExpiredTimer()
	local integer jumpTimerId = GetHandleId(jumpTimer)
	local unit jumper = LoadUnitHandle(udg_HT , jumpTimerId , 3)
	local location jumperLocation = GetUnitLoc(jumper)
	local real jumperX = GetLocationX(jumperLocation)
	local real jumperY = GetLocationY(jumperLocation) 
	local real dx = LoadReal(udg_HT , jumpTimerId , 4)
	local real dy = LoadReal(udg_HT , jumpTimerId , 5)
	local real process = LoadReal(udg_HT , jumpTimerId , 6)
	local location newLocation = null
	local location newRealLocation = null
	local boolean end = false
	//--
	if process < LoadReal(udg_HT , jumpTimerId , 7) then
		set process = process + 1.0
		call SaveReal(udg_HT , jumpTimerId , 6 , process)
		set newLocation = Location(jumperX + dx , jumperY + dy)
		call SetUnitPositionLoc(jumper , newLocation)
		set newRealLocation = GetUnitLoc(jumper)
		if DistanceBetweenPoints(newLocation , newRealLocation) > 5.0 then
			call SetUnitPositionLoc(jumper , jumperLocation)
			call SetUnitAnimationByIndex(jumper , 7)
			set end = true
		endif
	else
		set end = true
	endif
	if end then
		call PauseTimer(jumpTimer)
		call FlushChildHashtable(udg_HT , jumpTimerId)
		call DestroyTimer(jumpTimer)
		call PauseUnit(jumper , false)
		call SetUnitTimeScale(jumper , 1.0)
	endif
	//--
	call RemoveLocation(jumperLocation)
	call RemoveLocation(newLocation)
	call RemoveLocation(newRealLocation)
	set jumpTimer = null
	set jumper = null
	set jumperLocation = null
	set newLocation = null
	set newRealLocation = null
endfunction

function Trig_Jump_Actions takes nothing returns nothing
	local unit jumper = GetTriggerUnit()
	local location jumperLocation = GetUnitLoc(jumper)
	local real jumperX = GetLocationX(jumperLocation)
	local real jumperY = GetLocationY(jumperLocation)
	local location spellTargetLocation = GetSpellTargetLoc()
	local real angle = AngleBetweenPoints(jumperLocation , spellTargetLocation)
	local real distance = DistanceBetweenPoints(jumperLocation , spellTargetLocation)
	local real delta = 600.0 * 0.01 // speed setting
	local real dx = Cos(angle * bj_DEGTORAD) * delta
	local real dy = Sin(angle * bj_DEGTORAD) * delta
	local timer jumpTimer = CreateTimer()
	local integer jumpTimerId = GetHandleId(jumpTimer)
	local real maxDistance = 150.0 + LoadReal(udg_UnitDataHashTable , GetHandleId(jumper) , StringHash("JUMP_MASTERY")) * 8.0
	//--
	if distance > maxDistance then
		set distance = maxDistance
	endif
	call SaveUnitHandle(udg_HT , jumpTimerId , 3 , jumper)
	call SaveReal(udg_HT , jumpTimerId , 4 , dx)
	call SaveReal(udg_HT , jumpTimerId , 5 , dy)
	call SaveReal(udg_HT , jumpTimerId , 6 , 0.0) // process
	call SaveReal(udg_HT , jumpTimerId , 7 , distance / delta) // process MAX
	call TimerStart(jumpTimer , 0.01 , true , function JumpRoutine)
	call PauseUnit(jumper , true)
	call SetUnitTimeScale(jumper , 1200.0 / (distance + 100.0))
	call SetUnitAnimationByIndex(jumper , 30)
	call UnitUsedAbilityWithDelta(jumper , 'A009' , 1.0)
	//--
	call RemoveLocation(jumperLocation)
	call RemoveLocation(spellTargetLocation)
	set jumper = null
	set jumperLocation = null
	set spellTargetLocation = null
	set jumpTimer = null
endfunction


function Trig_Jump_Conditions takes nothing returns boolean
	return GetSpellAbilityId() == 'A009'
endfunction

function InitTrig_Jump takes nothing returns nothing
	local hashtable masteryTable = InitHashtable()
	local integer abilityId = 'A009'
	//--	
	//call SaveInteger(udg_AbilityDataHashTable , abilityId , StringHash("NEXT_ABILITY") , 'A01X')
	call SaveInteger(udg_AbilityDataHashTable , abilityId , StringHash("HOTKEY") , StringHash("Q"))
	call SaveInteger(udg_AbilityDataHashTable , abilityId , StringHash("MASTERY_ID") , StringHash("JUMP_MASTERY"))
	call SaveStr(udg_AbilityDataHashTable , abilityId , StringHash("NAME") , "緊急閃避")
	call SaveInteger(udg_AbilityDataHashTable , abilityId , StringHash("MAX_LEVEL") , 1)
	call SaveHashtableHandle(udg_AbilityDataHashTable , abilityId , StringHash("LEVEL_MASTERY_TABLE") , masteryTable)
	//call SaveReal(masteryTable , 1 , 0 , 1.0) //30
	//call SaveReal(masteryTable , 2 , 0 , 2.0) //70
	//call SaveReal(masteryTable , 3 , 0 , 3.0) //120
	
	set gg_trg_Jump = CreateTrigger()
	call TriggerRegisterAnyUnitEventBJ(gg_trg_Jump , EVENT_PLAYER_UNIT_SPELL_EFFECT)
	call TriggerAddCondition(gg_trg_Jump , Condition( function Trig_Jump_Conditions))
	call TriggerAddAction(gg_trg_Jump , function Trig_Jump_Actions)
endfunction